$(function(){
	anime_detail_index.run();
});

var anime_detail_index = {
	run: function(){
		this._init_();
		this._start_();
		this._bind_events();
	},
	
	_init_: function(){
		var loc = location.href;		
		this.category = 'all';
		var index = loc.indexOf("category");
		if(index >= 0){
			this.category = loc.substring(index + 9);
		}		
		this.genre = "all";
		
	},
	
	_start_: function(){
		this._highlight_nav();
		this._adjust_layout();
		if(this.category != "all"){
			$("#midlle_div").hide();
		}
		
		var loc = location.href;
		var index = loc.indexOf("#");
		if(index > 0){
			var params = loc.substring(index + 1);
			this._highlight_genre(params);
		}
	},
	
	_bind_events: function(){
		$(".img").live({
			mouseenter:function(){
				$(this).children('div.tc_btn').fadeIn("slow");
			},
			mouseleave:function(){
				$(this).children('div.tc_btn').stop(true,false).fadeOut("fast");
			}
		});
		
		$("div.tv_sMenu > ul > li > a ").live('click',function(){
			var divId = $(this).attr('divId');
			$(".displayorhide").css("display","none");
			$("#"+divId).css("display","block");
		});
		
		//genre
		$("#genres_ul > li > a").click(function(){
			var $this = $(this);
			var g = $this.attr("genre");			
			location.href = main.contextPath + "/world/anime/all#genre=" + g; 
			return false;
		});
		
		$(window).wresize(function() {
			anime_detail_index._adjust_layout();
		});
		
		$("#open_side_btn").click(function(){
			anime_detail_index._adjust_layout("open");
		});
		
		$("#close_side_btn").click(function(){
			anime_detail_index._adjust_layout();
		});
		
		anime_detail_index._handle_detail_to_play_location_param();
	},
	
	_highlight_nav: function(){
		var $li = $("#side_menu > ul > li");
		if(this.category == "recent"){
			$li.eq(2).addClass("curr");
		}else if(this.category == "simulacasts"){
			$li.eq(1).addClass("curr");
		}else{
			$li.eq(0).addClass("curr");
		}
	},
	
	_highlight_genre: function(params){
		var paramArr = params.split("&");
		for(var i=0; i<paramArr.length;i++){
			if (paramArr[i].indexOf("genre") >= 0) {
				var value = paramArr[i].substring(paramArr[i].indexOf("=") + 1);
				$("#genres_ul > li > a").removeClass("selected");
				$("#genres_ul > li > a").each(function() {
					if ($(this).attr("genre") == decodeURI(value)) {
						$(this).addClass("selected");
						anime_detail_index.genre = $(this).attr("genre");
					}
				});
			}
		}
	},
	
	//handle detail to play location param
	_handle_detail_to_play_location_param: function(){
		$("#an_se_box > div.displayorhide > div.tv_eCont > div.m_box > ul > li.name2 > a").live("click",function(){
			var href = $(this).attr("href");
			if(anime_detail_index.category == "all"){
				location.href = href + "#genre=" + anime_detail_index.genre + "&category=all";
			}else{
				location.href = href + "#category=" + anime_detail_index.category;
			}
			return false;
		});
		$("#an_se_box > div.displayorhide > div.tv_eCont > div.m_box > div.img > div.tc_btn > a").click(function(){
			var href = $(this).attr("href");
			if(anime_detail_index.category == "all"){
				location.href = href + "#genre=" + anime_detail_index.genre + "&category=all";
			}else{
				location.href = href + "#category=" + anime_detail_index.category;
			}
			return false;
		});
	},
	
	_adjust_layout: function(action){
		var boxWidth = $("#an_se_box").width()-40;
		if(action == 'open'){
			boxWidth = boxWidth - 180;
		}
		var account = Math.floor(boxWidth/165);
		var paddingValue = 20 + (boxWidth - 165*account + 20)/(account-1);
		$("#an_se_box > div.displayorhide").each(function(){
			var flag = 1;
			$(this).find("div.tv_eCont > div.m_box").each(function(){
				$(this).css("padding-right",0);
				if(flag < account){
					if(flag == 1){
						$(this).css("padding-left",20);
						flag++;
					}else{
						$(this).css("padding-left",paddingValue);
						flag++;
					}
				}else{
					$(this).css("padding-left",paddingValue);
					flag = 1;
				}
			});
		});
	}
};